package com.tyut.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.tyut.vo.BaseEntity;

import java.util.HashMap;
import java.util.Map;

public class AutoCoding {
    public static void main(String[] args) {
        String url = "jdbc:mysql://192.168.134.114:3306/GameDB";
        String username = "root";
        String password = "123456";
        String project = "GameBoot";
        String table = "address";
        String author = "李嘉昕";

        String basePath = System.getProperty("user.dir");
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder // 设置全局配置
                        .author(author)// 设置作者
                        .outputDir(basePath + "/" + project + "/src/main/java") // 指定输出目录
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir()
                )
                .packageConfig(builder -> {
                            Map<OutputFile, String> pathInfo = new HashMap<>();
                            pathInfo.put(OutputFile.xml, basePath + "/" + project + "/src/main/resources/com/tyut/mapper");  // 设置mapper.xml文件生成位置
                            pathInfo.put(OutputFile.entity, basePath + "/entity/src/main/java/com/tyut/entity");  // 设置mapper接口文件生成位置
                            builder  // 设置包配置
                                    .parent("com.tyut")
                                    .pathInfo(pathInfo); // 设置包名
                        }
                )
                .strategyConfig(builder -> builder  // 设置策略配置
                        .addInclude(table) // 设置需要生成的表名
                        .entityBuilder()  // 设置实体类配置
                        .naming(NamingStrategy.underline_to_camel) // 下划线到驼峰命名规则
                        .columnNaming(NamingStrategy.underline_to_camel) // 下划线到驼峰命名规则
                        .enableLombok() // 开启lombok, 生成实体类时自动添加lombok注解
                        .superClass(BaseEntity.class)
                        .addSuperEntityColumns("id") // 设置父类实体
                        .controllerBuilder() // 设置控制器配置
                        .enableRestStyle()  // 开启生成@RestController控制器
                        .serviceBuilder()
                        .formatServiceFileName("%sService") // 设置service接口名称格式
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
